package com.rst.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rst.dao.TenantConfigDAO;
import com.rst.model.MetaDataDTO;
import com.rst.model.TenantConfigMappingDTO;
import com.rst.model.TenantDTO;


@Service
public class TenantConfigService {

	@Autowired
	TenantConfigDAO configDAO;

	private Logger logger = Logger.getLogger(TenantConfigService.class);
	
	public List<MetaDataDTO> getMataDataList() {
		return configDAO.getMataDataList();
	}

	public Long saveTenant(HttpServletRequest request) {
		String name = request.getParameter("name");
		
		TenantDTO tenant  = new TenantDTO();
		String tenantId = configDAO.getLastTenantId();
		try {
			if(name != null && name.length()>0){
				if(tenantId == null){
					tenant.setTenantId("LNT-001");
				}else{
					String [] tid = tenantId.split("-");
					String tid1 =  tid[1];
					Integer tid2 = Integer.parseInt(tid1);
					String dbTenantId = tid[0]+"-00"+(++tid2); 
					tenant.setTenantId(dbTenantId);
				}
				tenant.setName(name);
				tenant.setStatus(TenantDTO.ACTIVE);
				
				Long id = configDAO.saveTenant(tenant);	
				return id;
			}else{
				logger.info("Eception At :: name="+name);			
				return (long) 0;
			}
		} catch (Exception e) {			
			e.printStackTrace();
			logger.info("Eception At :: name="+name);			
			return (long) 0;
		}

	}

	public List<TenantDTO> getTenantList() {
		return configDAO.getTenantList();
	}

	public Long saveMetaData(MetaDataDTO metaData) {
		return configDAO.saveMetaData(metaData);
	}

	public TenantDTO getTenanatById(Long tenantId) {
		return configDAO.getTenanatById(tenantId);		
	}

	public Long processTenantProvision(HttpServletRequest request) {
		
		
		String xml  ="";
		
		TenantConfigMappingDTO tenantXmlMapping = new TenantConfigMappingDTO();  
		List<MetaDataDTO> metaData = getMataDataList();
		
		String tenantId = request.getParameter("tenantId");
		String tenantName = request.getParameter("tName");
		
		xml = xml + "<configuration>";
		if(tenantId != null && tenantId.length()>0){
			xml = xml + "<tenantId>"+tenantId+"</tenantId>";
		}
		if(tenantName != null && tenantName.length()>0){
			xml = xml + "<tenantName>"+tenantName+"</tenantName>";
		}	
		for (MetaDataDTO data : metaData) {
			if(request.getParameter(data.getMetaData())!= null && request.getParameter(data.getMetaData()).length()>0){
				xml = xml + "<"+data.getMetaData()+">"+request.getParameter(data.getMetaData())+"</"+data.getMetaData()+">";	
			}
		}		
		xml = xml + "</configuration>";
		tenantXmlMapping.setTenantId(tenantId);
		tenantXmlMapping.setXmlConfig(xml);
		Long id = configDAO.saveMetaDataMappingDTO(tenantXmlMapping);
		return id;
	}

	public TenantConfigMappingDTO getXmlByTenantId(String tenantId) {
		return configDAO.getXmlByTenantId(tenantId);
	}

}
